<?php
/**
 * description
 *
 * @category   Module name
 * @package    PSR
 * @subpackage Documentation\API
 * @author     weiyizheng
 * @date       2022/11/8
 * @time       10:32 上午
 */

namespace Buu\Modules\EsStandard\Controllers;


use App\Models\DbInstance;
use App\Models\DbTables;
use Encore\Admin\Controllers\AdminController;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Illuminate\Http\Request;

class DbTablesController extends AdminController
{
	protected $title = "数据表组管理";

	public function index(Content $content)
	{
		$content->breadcrumb([
			'text' => $this->title
		]);
		return parent::index($content);
	}

	protected function grid()
	{
		$grid = new Grid(new DbTables());
		$grid->paginate(50);
		$grid->disableFilter();
		$grid->disableExport();
		$grid->column('id', 'ID');
		$grid->column('db_instance_id', '数据库实例')->display(function () {
			return DbInstance::find($this->db_instance_id)->name;
		});
		$grid->column('database', '数据库名称');
		$grid->column('tables', '数据表')->label()->width(200);
		$grid->column('table_name', '表名称')->label();
		$grid->column('primary_key', '主键字段');
		$grid->column('thread_num', '执行进程数');
		$grid->actions(function (Grid\Displayers\Actions $actions) {
			$actions->disableDelete();
		});
		return $grid;
	}

	protected function form()
	{
		$form = new Form(new DbTables());
		$form->footer(function (Form\Footer $footer) {
			$footer->disableEditingCheck();
			$footer->disableCreatingCheck();
			$footer->disableViewCheck();
		});
		$form->tools(function (Form\Tools $tools) {
			$tools->disableDelete();
			$tools->disableView();
		});
		$form->select('db_instance_id', '数据库实例')->options(
			DbInstance::all()->pluck(
				'name',
				'id'
			)
		)->load('database', '/admin/EventSystem/db-instance/getDatabases')->required();
		$form->select('database', '数据库名称')->options(function () {
				if ($this->db_instance_id) {
					return DbInstance::getDatabasesById($this->db_instance_id);
				} else {
					return [];
				}
			})->load('tables', '/admin/EventSystem/db-instance/getTables')->required();
		$form->multipleSelect('tables', '数据表')->options(function () {
			if ($this->db_instance_id) {
				return DbInstance::getTablesById($this->db_instance_id, $this->database);
			} else {
				return [];
			}
		})->required();
		$form->text('table_name', '表名称')->setWidth(4, 2)->required();
		$form->text('primary_key', '主键字段')->setWidth(4, 2)->default('id')->required();
		$form->number('thread_num', '执行进程数')->setWidth(4, 2)->default('1')->max(20)->required();
		$form->hidden('add_time');
		$form->hidden('update_time');
		$form->hidden('add_username');
		$form->hidden('update_username');
		$form->saving(function (Form $form) {
			if ($form->isCreating()) {
				$form->add_time = time();
				$form->add_username = Admin::user()->username;
			}
			$form->update_time = time();
			$form->update_username = Admin::user()->username;
		});
		return $form;
	}

	public function getColumns(Request $request)
	{
		$id = $request->get('q');
		if (!$id) {
			return [];
		}
		$dbTable = DbTables::find($id)->toArray();
		return DbInstance::getColumnsById($dbTable['db_instance_id'], $dbTable['database'], $dbTable['tables'][0]);
	}
}